<?php
declare (strict_types=1);

namespace app\backend\controller\v1;

use app\backend\validate\ArticleValidate;
use app\common\model\Article as ArticleModel;
use think\exception\ValidateException;
use think\Request;

class Article extends BaseController
{
    /**
     * 会员列表
     *
     * @return \think\Response
     */
    public function index()
    {
        // 会员表
        $model = new ArticleModel();
        // where条件
        $where = [];
//        $status = input('get.status/d', 1);
//        if (!empty($status)) {
//            $where[] = ['status', '=', $status];
//        }
        $title = input('get.title/s', '');
        if (!empty($title)) {
            $where[] = ['title', 'like', '%' . $title . '%'];
        }
        // 分页、排序等
        $page = input('page/d', 1);
        $pageSize = input('pageSize/d', 15);
        $sort = ['id' => 'desc'];
        $field = 'id,user_id,title,created_at';
        //获取列表
        $list = ArticleModel::where($where)->field($field)->page($page, $pageSize)->order($sort)->select();
        $allTotal = ArticleModel::where($where)->count();
        return sucessMsg(200, '文章列表获取成功！', ['list' => $list, 'total' => count($list), 'allTotal' => $allTotal]);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        if (request()->isPost()) {
            $requestData = input('post.');
            try {
                //  验证参数
                validate(ArticleValidate::class)->scene('create')->check($requestData);
                //新增会员
                $user = new ArticleModel();
                $user->title = $requestData['title'];
                $user->user_id = $this->user_id;
                if ($user->save()) {
                    return sucessMsg(200, '创建成功！', $user->id);
                }
                return errMsg(500, '创建失败！');
            } catch (ValidateException $e) {
                return errMsg(400, $e->getError());
            }
        }
        return errMsg(500, '请求错误！');
    }

    /**
     * 保存更新的资源
     *
     * @param \think\Request $request
     * @param int $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        if (request()->isPut()) {
            $requestData = input();
            try {
                //  验证参数
                validate(ArticleValidate::class)->scene('update')->check($requestData);
                //新增会员
                $user = ArticleModel::find($requestData['id']);
                $user->title = $requestData['title'];
                $user->user_id = $this->user_id;
                if ($user->save()) {
                    return sucessMsg(200, '修改成功！', $user->id);
                }
                return errMsg(500, '修改失败！');
            } catch (ValidateException $e) {
                return errMsg(400, $e->getError());
            }
        }
        return errMsg(500, '请求错误！');
    }

    /**
     * 删除指定资源
     *
     * @param int $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id = input('id/d', 0);
        if (empty($id)) {
            return errMsg(400, '请选择要删除用户id！');
        }
        if (ArticleModel::destroy($id)) {
            return sucessMsg(200, '删除成功！');
        }
        return errMsg(500, '删除失败！');
    }
}
